function MakePPTforStaging( sDir )
%MakePPTforStaging creates a powerpoint to facilitate staging
    function aiXYReturn = SearchStage(cellData)
        %Search for 'Stage:' in cell array
        if (strcmp(cellData{10,1},'Stage:'))
            aiXYReturn = [10,1];
            return
        end;
        for iRow=1:size(cellData,1)
            for iCol=1:size(cellData,2)
                if (strcmp(cellData{iRow,iCol},'Stage:'))
                    aiXYReturn = [iRow, iCol];
                    return
                end;
            end;
        end;
    end

    function aiXYReturn = SearchSourceFile(cellData)
        %Search for 'Stage:' in cell array
        if (strcmp(cellData{6,1},'SourceFiles:'))
            aiXYReturn = [6,1];
            return
        end;
        for iRow=1:size(cellData,1)
            for iCol=1:size(cellData,2)
                if (strcmp(cellData{iRow,iCol},'SourceFiles:'))
                    aiXYReturn = [iRow, iCol];
                    return
                end;
            end;
        end;
    end

    function aiXYReturn = SearchVoxelSize(cellData)
        %Search for 'Size of Voxels' in cell array
        if (strcmp(cellData{7,1},'Size of Voxels'))
            aiXYReturn = [7,1];
            return
        end;
        for iRow=1:size(cellData,1)
            for iCol=1:size(cellData,2)
                if (strcmp(cellData{iRow,iCol},'Size of Voxels'))
                    aiXYReturn = [iRow, iCol];
                    return
                end;
            end;
        end;
    end
    
    function dsReturn = Crawler (sDir)
        %Crawl through all files and sub dirs
        lList  = dir(sDir);
        dsReturn=dataset();
        for iFile = 1:length(lList)
            sFilename = lList(iFile).name;
            if (lList(iFile).isdir)
                if (~strcmp(sFilename,'.') && ~strcmp(sFilename,'..'))
                    dsReturn=vertcat(dsReturn,Crawler([sDir '\' sFilename]));
                end;
            %Is it a valid excel file?, xlsfinfo has problems with some
            %files, so check the extension first
            elseif (strcmp('.xls',sFilename(end-3:end)) && ~strcmp(xlsfinfo([sDir '\' sFilename]),''))
                %Check if it is the newest version
                sSearchString = regexprep(sFilename,'\d{6}','*');
                lTempList = dir([sDir '\' sSearchString]);
                if (~strcmp(lTempList(end).name,sFilename))
                    continue;
                end;
                %Read data from the Excel file
                try
                    [~,~,cellData] = xlsread([sDir '\' sFilename],'Data');
                catch
                    disp(['Strange Excel sheet couldn''t be opened ' sDir '\' sFilename]);
                    continue;
                end;
                
                %Find where stage and source file are
                aiStage  = SearchStage(cellData)+ [0,1];
                aiSource = SearchSourceFile(cellData) + [0,1];
                aiVoxelSize=SearchVoxelSize(cellData) + [1,1];
                
                struFiles.thumb = [sDir '\' sFilename(1:end-3) 'thumb.tif'];
                struFiles.xls   = [sDir '\' sFilename];
                struFiles.ims   = cellData{aiSource(1,1),aiSource(1,2)};
                
                dXRes = cellData{aiVoxelSize(1,1),aiVoxelSize(1,2)};
                dYRes = cellData{aiVoxelSize(1,1),aiVoxelSize(1,2)+1};
                
                %Datasets can't cope with different variable types, so
                %convert all stages to strings of same length
                if (isnumeric(cellData{aiStage(1,1),aiStage(1,2)}))
                    sStage = sprintf('%-40s',num2str(cellData{aiStage(1,1),aiStage(1,2)}));
                else
                    sStage = sprintf('%-40s',strtrim(cellData{aiStage(1,1),aiStage(1,2)}));
                end; 
                sStage=sStage(1:40);
                
                dsTemp  = dataset({sStage,'Stage'}, {struFiles,'Files'}, {dXRes,'XRes'}, {dYRes,'YRes'});
                dsReturn= vertcat(dsReturn, dsTemp);
            end;
        end;
        return;
    end

    
    %Check if sDir is present if not let user choose Directory
    if (~exist('sDir','var'))
        sDir = uigetdir('C:\','Choose a directory that should be parsed for the Powerpoint presentation');
        if (isequal(sDir,0))
            return;
        end;
    end;
    if (~isdir(sDir))
        return;
    end;
    dsData = Crawler(sDir);
    %Don't continue if no files were found
    if isempty(dsData)
        return;
    end;
    %%Sort Dataset by stage
    %[~,aiIndex]=sortrows(dsData(:,'Stage'));
    %dsData=dsData(aiIndex,:);
    
    %%
    %Okay, now that we have the summary let's do an output
    apPPT = actxserver('PowerPoint.Application');
    Presentation = apPPT.Presentations.Add;
    
    %Create a temporary file
    sTempImage = [tempname '.jpeg'];
    
    for iData = 1:length(dsData)
        NewSlide = Presentation.Slides.invoke('Add',Presentation.Slides.Count+1,11);
        NewSlide.Shapes.Title.TextFrame.TextRange.Text='XLS IMS Choose a stage';
        
        ShapeTable = NewSlide.Shapes.AddTable(3,2);
        %Make the table bigger
        ShapeTable.Left  = 20;
        ShapeTable.Width = Presentation.PageSetup.SlideWidth/2-30; 
        %Set table header
        ShapeTable.Table.Cell(1,1).Shape.TextFrame.TextRange.Text='Type';
        ShapeTable.Table.Cell(1,2).Shape.TextFrame.TextRange.Text='Filename';
        ShapeTable.Table.Cell(2,1).Shape.TextFrame.TextRange.Text='XLS';
        ShapeTable.Table.Cell(2,2).Shape.TextFrame.TextRange.Text=strtrim(dsData{iData,'Files'}.xls);
        ShapeTable.Table.Cell(3,1).Shape.TextFrame.TextRange.Text='IMS';
        ShapeTable.Table.Cell(3,2).Shape.TextFrame.TextRange.Text=strtrim(dsData{iData,'Files'}.ims);
        
        %Try to import image into Powerpoint
        try
            aiImage=imread(dsData{iData,'Files'}.thumb);
            NewSlide.Shapes.Title.TextFrame.TextRange.Text=['XLS IMS ' num2str(size(aiImage,2)*dsData{iData,'YRes'},'%1.f') 'x' num2str(size(aiImage,1)*dsData{iData,'XRes'},'%1.f') ' um   ' num2str(iData) '/' num2str(length(dsData))];
            aiImage=uint8(512/double(max(max(aiImage)))*aiImage);
            imwrite(aiImage,sTempImage,'jpeg');
            %Determine whether it will fit on the slide
            dWidth = Presentation.PageSetup.SlideWidth/2-10;
            dHeight = dWidth*size(aiImage,1)/size(aiImage,2);
            if (dHeight>Presentation.PageSetup.SlideHeight-100)
                dHeight = Presentation.PageSetup.SlideHeight-100;
                dWidth  = dHeight * size(aiImage,2)/size(aiImage,1);
            end;
            NewSlide.Shapes.AddPicture(sTempImage,false, true, Presentation.PageSetup.SlideWidth/2,100, dWidth ,dHeight );
        catch error
            disp(error.message);
            disp(['Couldn''t add image ' dsData{iData,'Files'}.thumb]);
        end;
        
        %Set Hyperlinks in title
        %For some strange reason it won't work within a table
        NewSlide.Shapes.Title.TextFrame.TextRange.Characters(1,3).ActionSettings(1).Item(1).Hyperlink.Address=['file:\\' strtrim(dsData{iData,'Files'}.xls)];
        NewSlide.Shapes.Title.TextFrame.TextRange.Characters(5,3).ActionSettings(1).Item(1).Hyperlink.Address=['file:\\' strtrim(dsData{iData,'Files'}.ims)];
        
        %Add Stage
        NewSlide = Presentation.Slides.invoke('Add',Presentation.Slides.Count+1,11);
        NewSlide.Shapes.Title.TextFrame.TextRange.Text=['That was stage ' strtrim(dsData{iData,'Stage'})];
        
    end;
    delete(sTempImage);
    %Make Powerpoint visible
    apPPT.visible=true;



end

